﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Cauhinh : System.Web.UI.Page
{
    BieumauDataContext db = new BieumauDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["login_admin"] == null)
        {
            Response.Redirect("Index.aspx");
        }
    }
    
    /// <summary>
    /// 
    /// </summary>
    public void add()
    {
        Mail_system email = new Mail_system();
        email.ID = 1;
        email.Mail = txtemail_new.Text.Trim();
        email.Pass = txtmatkhau.Text.Trim();
        email.Chude = txtchude.Text;
        email.Noidung = txtnoidung.Text;
        email.Tenhienthi = txttenhienthi.Text;
        db.Mail_systems.InsertOnSubmit(email);
        db.SubmitChanges();
    }
    /// <summary>
    /// hàm thêm mới email hệ thống
    /// </summary>
    /// <param name="id">mã địa chỉ email</param>
    public void update(int id)
    {
        Mail_system email = db.Mail_systems.Single(p => p.ID == id);
        email.Mail = txtemail_new.Text.Trim();
        email.Pass = txtmatkhau.Text.Trim();
        email.Chude = txtchude.Text;
        email.Noidung = txtnoidung.Text;
        email.Tenhienthi = txttenhienthi.Text;
        db.SubmitChanges();

    }
    public void xoathongtin()
    {
        txtchude.Text="";
        txtemail_new.Text="";
        txtmatkhau.Text="";
        txtnoidung.Text="";
        txtre_matkhau.Text="";
        txttenhienthi.Text="";

      
    }
    /// <summary>
    /// hàm cập nhật email hệ thống
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_xoa_Click(object sender, EventArgs e)
    {

        if (Page.IsValid)
        {
            if (txtemail_new.Text.Contains("@gmail"))
            {
                int a = db.Mail_systems.Count();
                if (a != 0)
                {

                    update(1);
                    xoathongtin();
                }
                else
                {
                    add();

                }
            }
            else
            {
                lbl_status.ForeColor = System.Drawing.Color.Red;
                lbl_status.Text = "Email bắt buộc phải là Gmail có dạng : Tendiachi@gmail.com";
            }
        }
    }
}